//
//  TTOSSServer.swift
//  TTServer
//
//  Created by Toj on 4/14/21.
//

import Foundation

public enum OSSACLType: String {
    case pbrw = "public-read-write"
    case pbr = "public-read"
    case `private` = "private"
    
    static var `default`: Self {
        return OSSACLType.pbrw
    }
}

open class TTOSSServer: NSObject {
    fileprivate var service: TTOSSService!
    
    private override init() { }
    
    public init(endpoint: String, serverUrl: String) {
        super.init()
        
        service = TTOSSService(endpoint: endpoint, serverUrl: serverUrl)
    }
    
    public init(endpoint: String, accessKey: String, secretKey: String, token: String, expiration: String) {
        super.init()
        
        service = TTOSSService(endpoint: endpoint, accessKey: accessKey, secretKey: secretKey, token: token, expiration: expiration)
    }
}

// MARK: - upload
/** 内存中上传或本地上传文件. 最大不能超过5GB.
 bucketName: 存储空间名称
 objectKey == objectName: 表示将文件上传到OSS时需要指定包含文件后缀在内的完整路径
 例如: abc/efg/123.jpg.
 */
public extension TTOSSServer {
    
    func upload(bucket name: String,
                object keyPath: String,
                from url: String,
                progress: TTOSSUploadProgressBlock? = nil,
                completed: TTOSSUploadCompletedBlock? = nil) {
        service.upload(bucket: name, object: keyPath, from: url, progress: progress, completed: completed)
    }
    
    func upload(bucket name: String,
                object keyPath: String,
                data: Data,
                progress: TTOSSUploadProgressBlock? = nil,
                completed: TTOSSUploadCompletedBlock? = nil) {
        service.upload(bucket: name, object: keyPath, data: data, progress: progress, completed: completed)
    }
}

// MARK: - download
/** 可以指定下载为本地文件或者下载为Data
 bucketName: 存储空间名称
 objectKey == objectName: 表示将文件上传到OSS时需要指定包含文件后缀在内的完整路径
 例如: abc/efg/123.jpg.
 */
public extension TTOSSServer {
    func download(bucket name: String,
                  object keyPath: String,
                  to url: String = "",
                  progress: TTOSSDownloadProgressBlock? = nil,
                  completed: TTOSSDownloadCompletedBlock? = nil) {
        service.download(bucket: name, object: keyPath, to: url, progress: progress, completed: completed)
    }
}
